<?php

namespace IDock\Insurance\ZFIC\Request;

use Exception;
use IDock\Insurance\ZFIC\Response\InsuranceConfirmResponse;
use IDock\Insurance\ZFIC\ZFICEnums;
use IDock\Printer\NonCar\NonCarProductPrinter;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\SpecialClausesVisitor;

class InsuranceConfirmRequest extends AllZFICRequest
{
    protected $urlTip = '核保接口';

    protected $route = 'order/api/order/carUnderwrit';

    protected $response = InsuranceConfirmResponse::class;

    /**
     * @var PremiumInquiryVisitor $visitor
     */
    protected $visitor;

    /**
     * @return array
     * @throws Exception
     * @version 4.0
     */
    public function generate(): array
    {
        $param = [
            'businessNo' => $this->visitor->getExtInfoVisitor()->argc['businessNo'],
            'platOrderNo' => $this->visitor->getExtInfoVisitor()->argc['platOrderNo'],
            'appliInfo' => $this->getApplicantInfo(),
            'insuredInfo' => $this->getInsuredInfo(),
            'coClauseList' => [],//特约列表
            'salesmanAdvice' => $this->visitor->getExtInfoVisitor()->underwriteMsg,
//            'uwCountJY' => $this->uwCountJY,// 驾意险必填
//            'quantityJY' => $this->quantityJY,
//            'startDateJY' => $this->startDateJY,
//            'endDateJY' => $this->endDateJY,
//            'chalCode' => $this->chalCode,
//            'userCode' => $this->userCode,
//            'productCode' => $this->productCode,
//            'planCode' => $this->planCode,
            'feeFlag' => '1'
        ];
        $this->nonCarInfo($param);
        if(!empty($this->visitor->getUnderwritingVisitor()->specialClausesList)){
            $coClauseList = [];
            foreach ($this->visitor->getUnderwritingVisitor()->specialClausesList as $value){
                /**@var SpecialClausesVisitor $value * */
                $coClauseList[] = [
                    'platOrderNo' => $this->visitor->getExtInfoVisitor()->argc['platOrderNo'],
                    'chalOrderNo' => $this->visitor->getExtInfoVisitor()->argc['businessNo'],
                    'lineNo' => 1,
                    'serialNo' => 1.0,
                    'clauseCode' => $value->clauseCode,
                    'clauseName' => $value->clauseCode,
                    'riskCode' => $value->riskType == 'TCI' ? '0801' : '0808',
                    'context' => $value->clauseContent,
                ];
            }
            $param['coClauseList'] = $coClauseList;
        }
        return $param;
    }


    /**
     * @throws Exception
     */
    private function getApplicantInfo(): array
    {
        $applicant = $this->visitor->getApplicantVisitor();
        $arr = [
            "appliName" => $applicant->name,
            "identifyType" => ZFICEnums::$certificateTypeCode[$applicant->certificateTypeCode],
            "identifyNumber" => $applicant->certificateNo,
            "sex" => '',
            "birthDate" => "",
            "mobileNumber" => $applicant->mobile,
            "applicantType" => $applicant->typeCode == 'R1' ? '1' : '2',////1:个人 2:团体, 投保人类型
            "workUnit" => "",
            "address" => $applicant->address,
            "postCode" => "610000",//邮政编码
            "officePhone" => "",//单位电话 团体必传
            "contactName" => "",//联系人 团体必传
            "contactMobile" => "",//联系人电话 团体必传
            "email" => $applicant->email,
            "idcardInstitutions" => "四川省成都市公安局",//签发机构
            "nationality" => "汉",
            "identifyStartDate" => $applicant->certificateStartDate,
            "identifyEndDate" => $applicant->certificateEndDate,
            "identifyNature" => "1",//车辆所有人 1 车辆管理人 2 其它  3
            "contactIdentifyType" => "1",//1为身份证
            "contactIdentifyNumber" => $applicant->certificateNo,
            "contactSex" => "",
        ];
        if(strlen($applicant->mobile) > 11){
            throw new Exception("投保人手机号不能超过11位");
        }

        //驾意险参数
        if ($this->visitor->getNonCarUnderwritingVisitor()->getNonCarList()) {
            $arr['itemProvinceCode'] = '510000';
            $arr['itemCityCode'] = '510100';
            $arr['itemProvinceCName'] = '四川省';
            $arr['itemCityCName'] = '成都市';
        }

        if ($applicant->typeCode == 'R1') {
            $demo = new OrderPriceQueryRequest();
            $arr['sex'] = $arr['contactSex'] = $demo->getGenderByIDCard($applicant->certificateNo);
            $arr['birthDate'] = $demo->getBirthday($applicant->certificateNo);
        }
        if ($arr['applicantType'] == '2') {
            //团体
            $arr['officePhone'] = $applicant->mobile;
            $arr['contactName'] = $applicant->name;
            $arr['contactMobile'] = $applicant->mobile;
        }
        if (empty($arr['identifyType'])) {
            throw new Exception("不支持的投保人证件类型");
        }
        return $arr;
    }

    /**
     * @throws Exception
     */
    private function getInsuredInfo(): array
    {
        $insured = $this->visitor->getInsuredVisitor();
        $arr = [
            "insuredName" => $insured->name,
            "identifyType" => ZFICEnums::$certificateTypeCode[$insured->certificateTypeCode],
            "identifyNumber" => $insured->certificateNo,
            "sex" => "",
            "birthDate" => "",
            "mobileNumber" => $insured->mobile,
            "insuredType" => $insured->typeCode == 'R1' ? '1' : '2',////1:个人 2:团体, 投保人类型
            "insuredAddress" => $insured->address,
            "postCode" => "610000",
            "officePhone" => "",
            "contactName" => "",
            "contactMobile" => "",
            "carInsuredRelation" => "1",//被保险人与车辆关系 默认：1-拥有
            "email" => $insured->email,
            "idcardInstitutions" => "市公安局",
            "nationality" => "汉",
            "identifyStartDate" => $insured->certificateStartDate,
            "identifyEndDate" => $insured->certificateEndDate,
            "insuredNature" => "1"//车辆所有人  1
        ];
        if(strlen($insured->mobile) > 11){
            throw new Exception("被保人手机号不能超过11位");
        }
        if ($insured->typeCode == 'R1') {
            $demo = new OrderPriceQueryRequest();
            $arr['sex'] = $arr['contactSex'] = $demo->getGenderByIDCard($insured->certificateNo);
            $arr['birthDate'] = $demo->getBirthday($insured->certificateNo);
        }
        if ($arr['insuredType'] == '2') {
            //团体
            $arr['officePhone'] = $insured->mobile;
            $arr['contactName'] = $insured->name;
            $arr['contactMobile'] = $insured->mobile;
        }
        if (empty($arr['identifyType'])) {
            throw new Exception("不支持的被保人证件类型");
        }
        return $arr;
    }

    /**
     * 驾意险产品信息
     * @param $param
     * @return void
     * @throws Exception
     * @version 4.0
     */
    private function nonCarInfo(&$param)
    {
        $nonCarVisitor = $this->visitor->getNonCarUnderwritingVisitor();

        $nonCarList = $nonCarVisitor->getNonCarList();

        if (!count($nonCarList)) {
            return;
        }
        if (count($nonCarList) > 1) {
            throw new Exception('只能选择一个驾意险产品');
        }

        /*** @var $nonCarProduct NonCarProductPrinter */
        $nonCarProduct = array_shift($nonCarList);

        $param['startDateJY'] = $nonCarVisitor->startData;
        $param['endDateJY'] = date('Y-m-d H:i:s', strtotime('-1 second', strtotime($nonCarVisitor->endData)));
        $param['uwCountJY'] = (int)$nonCarProduct->insuredNum;
        $param['quantityJY'] = (int)$this->visitor->getVehicleVisitor()->vehicleSeat;
        $param['chalCode'] = $nonCarVisitor->argc['NCChalCode'] ?? '';
        $param['userCode'] = $nonCarVisitor->argc['NCUserCode'] ?? '';
        $param['productCode'] = $nonCarProduct->productCode;
        $param['planCode'] = $nonCarProduct->planCode;
    }

    public function getStaffConfig(&$chalCode, &$userCode)
    {
        $chalCode = $this->visitor->getExtInfoVisitor()->argc['chalCode'] ?? '';
        $userCode = $this->visitor->getExtInfoVisitor()->argc['userCode'] ?? '';
    }
}
